using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace phasor
{
	/// <summary>
	/// Summary description for Form2.
	/// </summary>
	public class Form2 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button butZeroSpeed;
		public System.Windows.Forms.TrackBar trackSpeed;
		private System.Windows.Forms.Label labSpeed;

		private System.ComponentModel.Container components = null;

		public const string labSpeedPre = "Speed:";
		public const string labSpeedPost = "";

		private Form1 form1;
		public System.Windows.Forms.Button butPause;


		
		private bool trackSpeedJustSet = false;
		public bool nJustSet = false;
		public bool phaseJustSet = false;
		private bool trackZoomJustSet = false;
		

		public Form2() {
			InitializeComponent();
		}

		public void SyncFromForm1() {

			// Set speed slider
			trackSpeedJustSet = true;
			trackSpeed.Value = dAlpha_to_trackSpeed(form1.dAlpha);
			
			// Sync pause button
			UpdatePauseInfo();
		}

		



		
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.trackSpeed = new System.Windows.Forms.TrackBar();
            this.butPause = new System.Windows.Forms.Button();
            this.labSpeed = new System.Windows.Forms.Label();
            this.butZeroSpeed = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.trackSpeed)).BeginInit();
            this.SuspendLayout();
            // 
            // trackSpeed
            // 
            this.trackSpeed.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.trackSpeed.AutoSize = false;
            this.trackSpeed.LargeChange = 1;
            this.trackSpeed.Location = new System.Drawing.Point(-6, 21);
            this.trackSpeed.Maximum = 1000;
            this.trackSpeed.Minimum = -1000;
            this.trackSpeed.Name = "trackSpeed";
            this.trackSpeed.Size = new System.Drawing.Size(477, 23);
            this.trackSpeed.TabIndex = 12;
            this.trackSpeed.TickStyle = System.Windows.Forms.TickStyle.None;
            this.trackSpeed.ValueChanged += new System.EventHandler(this.trackSpeed_ValueChanged);
            this.trackSpeed.KeyDown += new System.Windows.Forms.KeyEventHandler(this.keyDown);
            // 
            // butPause
            // 
            this.butPause.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.butPause.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butPause.Location = new System.Drawing.Point(416, 1);
            this.butPause.Name = "butPause";
            this.butPause.Size = new System.Drawing.Size(47, 18);
            this.butPause.TabIndex = 13;
            this.butPause.Text = "Pause";
            this.butPause.Click += new System.EventHandler(this.butPause_Click);
            this.butPause.KeyDown += new System.Windows.Forms.KeyEventHandler(this.keyDown);
            // 
            // labSpeed
            // 
            this.labSpeed.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labSpeed.Location = new System.Drawing.Point(0, 3);
            this.labSpeed.Name = "labSpeed";
            this.labSpeed.Size = new System.Drawing.Size(48, 16);
            this.labSpeed.TabIndex = 14;
            this.labSpeed.Text = "Speed:";
            // 
            // butZeroSpeed
            // 
            this.butZeroSpeed.Anchor = System.Windows.Forms.AnchorStyles.Top;
            this.butZeroSpeed.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.butZeroSpeed.Location = new System.Drawing.Point(221, 1);
            this.butZeroSpeed.Name = "butZeroSpeed";
            this.butZeroSpeed.Size = new System.Drawing.Size(26, 18);
            this.butZeroSpeed.TabIndex = 15;
            this.butZeroSpeed.Text = "0";
            this.butZeroSpeed.Click += new System.EventHandler(this.butZeroSpeed_Click);
            this.butZeroSpeed.KeyDown += new System.Windows.Forms.KeyEventHandler(this.keyDown);
            // 
            // Form2
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(466, 46);
            this.Controls.Add(this.butZeroSpeed);
            this.Controls.Add(this.labSpeed);
            this.Controls.Add(this.butPause);
            this.Controls.Add(this.trackSpeed);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.MaximizeBox = false;
            this.Name = "Form2";
            this.Opacity = 0.25;
            this.ShowInTaskbar = false;
            this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
            this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
            this.Load += new System.EventHandler(this.Form2_Load);
            this.Closing += new System.ComponentModel.CancelEventHandler(this.Form2_Closing);
            ((System.ComponentModel.ISupportInitialize)(this.trackSpeed)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		private void butZeroSpeed_Click(object sender, System.EventArgs e) {
			trackSpeed.Value = 0;
			
		}

		protected override bool ProcessDialogKey(Keys key) {
			return form1.ProcessDialogKey_Internal(key);
		}

		private void keyDown(object sender, System.Windows.Forms.KeyEventArgs e) {
			e.Handled = ProcessDialogKey(e.KeyCode);
		}

		public static double TrackSpeed_to_dAlpha(int speed) {
			double dAlpha = speed * speed / 2200000000.0;
			if (speed < 0) {
				dAlpha *= -1;
			}
			return dAlpha;
		}

		public static int dAlpha_to_trackSpeed(double dAlpha) {
			int speed =  (int)Math.Round(Math.Pow(Math.Abs(dAlpha) * 2200000000.0, 0.5), 0);
			if (dAlpha < 0) {
				speed *= -1;
			}
			return speed;
		}

		public void SetPositionOnOwner() {
			//Location = form1.PointToScreen(new Point(153, 435));
			this.Width = form1.graph1.Width - 165;
			
			this.Location = form1.PointToScreen(
				new Point(form1.ClientRectangle.Width - this.Width - 10 , 
				form1.ClientRectangle.Height - this.Height - 5));


		}


		private void Form2_Load(object sender, System.EventArgs e) {
			form1 = (Form1)this.Owner;
			SetPositionOnOwner();

			SyncFromForm1();
		}

		private void trackSpeed_ValueChanged(object sender, System.EventArgs e) {
			if (!trackSpeedJustSet) {
				// Set acutal dAlpha after translating from trackSpeed
				((Form1)this.Owner).dAlpha = TrackSpeed_to_dAlpha(trackSpeed.Value);
			} else {
				trackSpeedJustSet = false;
			}
		}

		private void butPause_Click(object sender, System.EventArgs e) {
			form1.Pause = !form1.Pause;	
			UpdatePauseInfo();
		}

		public void UpdatePauseInfo() {
			if (form1.Pause) {
				butPause.Text = "Start";
			} else {
				butPause.Text = "Pause";
			}
		}





		public bool Closed = false;

		private void Form2_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
			Closed = true;
		}









	}
}
